package fun.cyhgraph.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import fun.cyhgraph.exception.BusinessException;
import fun.cyhgraph.exception.ThrowUtils;
import fun.cyhgraph.model.dto.CategoryDTO;
import fun.cyhgraph.model.dto.CategoryTypePageDTO;
import fun.cyhgraph.model.entity.Category;
import fun.cyhgraph.mapper.CategoryMapper;
import fun.cyhgraph.model.entity.User;
import fun.cyhgraph.model.merchantDto.CategoryTypePageMerchantDTO;
import fun.cyhgraph.result.PageResult;
import fun.cyhgraph.service.CategoryService;
import fun.cyhgraph.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private UserService userService;

    /**
     * 新增分类
     * @param categoryDTO
     */
    public void addCategory(CategoryDTO categoryDTO , Integer merchantId) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(1);  // 默认启用
        category.setCreateUser(merchantId);
        category.setMerchantId(merchantId);
        category.setUpdateUser(merchantId);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.add(category);
    }

    /**
     * 根据type条件分页查询
     * @param categoryTypePageDTO
     * @return
     */
    public PageResult getPageList(CategoryTypePageDTO categoryTypePageDTO , HttpServletRequest request) {
        PageHelper.startPage(categoryTypePageDTO.getPage(), categoryTypePageDTO.getPageSize());
        User loginUser = userService.getLoginUser(request);
        CategoryTypePageMerchantDTO categoryTypePageMerchantDTO = new CategoryTypePageMerchantDTO();
        BeanUtils.copyProperties(categoryTypePageDTO , categoryTypePageMerchantDTO);

        Page<Category> pagelist = categoryMapper.getPageList(categoryTypePageMerchantDTO);
        return new PageResult(pagelist.getTotal(), pagelist.getResult());
    }

    /**
     * 获取所有分类列表
     * @return
     */
    public List<Category> getList(Integer type) {
        List<Category> categoryList = categoryMapper.getList(type);
        return categoryList;
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    public Category getById(Integer id) {
        return categoryMapper.getById(id);
    }

    /**
     * 更新分类信息
     * @param categoryDTO
     */
    public void udpate(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.update(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    public void delete(Integer id) {
        categoryMapper.delete(id);
    }

    @Override
    public boolean  deleteCategory(Integer id, Integer merchantId) {
        boolean delete = categoryMapper.deleteCategory(id, merchantId);
        return delete;
    }

    /**
     * 分类起售/停售
     */
    public void onOff(Integer id , Integer merchantId) {
        boolean onOff = categoryMapper.onOff(id, merchantId);
        if (!onOff) throw new BusinessException(50001 , "操作失败");
    }
}
